package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.ArticleModel;
import com.app.model.MotionVideoModel;
import com.app.service.AdminUserService;
import com.app.service.ArticleService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 文章管理
 */
@Controller
@RestController
@RequestMapping("/admin")
public class ArticleController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private ArticleService articleService;

    //新增和编辑
    @PostMapping("/insertAndUpdateArticle")
    public Result<Object> insertAndUpdateArticle(@RequestBody ArticleModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            articleService.update(model);
        } else {
            model.setCreateTime(CommonHelp.getNowTimestamp());
            articleService.insert(model);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteArticle")
    public Result<Object> deleteArticle(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        articleService.deleteById(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getArticleList")
    public Result<Object> getArticleList(Integer page, Integer limit, String keyword, Integer township, Integer type, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(articleService.selectList(page, limit, keyword, township, type));
    }

    //查看详情
    @GetMapping("/getArticleInfo")
    public Result<Object> getArticleInfo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(articleService.selectById(id));
    }

    //更新状态
    @GetMapping("/updateArticleStatus")
    public Result<Object> updateArticleStatus(Integer id, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        ArticleModel model = new ArticleModel();
        model.setId(id);
        model.setStatus(status);
        articleService.update(model);
        return Result.success("更新成功");
    }
}
